var app=getApp()
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    sexArray: [{
      title: '男',
      id: 'man'
    },
      {
        title: '女',
        id: 'woman'
      }],
      shenfenArray:[
       
      ],
      subjectArray:[
     
      ],
    holder:'可填入学习经历、获奖经历、教学经历、个人爱好、性格等。',
    address: '选择您的地址',
    phoneNumber: '短信验证',
    titleStyle: 'font-size:15px; color:#434343;font-weight:normal;',
    imgStyle: 'width:28rpx; height:34rpx;',
    phoneStyle: 'font-size:15px; color:#626262;font-weight:normal;',
    name:'',
    sex:'',
    universty:'',
    shenfeng:'',
    date:'',
    subject:'',
    imgPath:'',
    introduce:'',
    url:'',
    motto:''
  },
  getName:function(event){
    console.log(event)
    this.setData({
      name:event.detail.value
    })
  },
  getSex:function(event){
    this.setData({
      sex:event.detail.id
    })
  },
  getUniversty:function(event){
     this.setData({
        universty:event.detail.value
     })
  },
  getShenfeng:function(event){
    var shenfeng = event.detail.name
     console.log(event)
     this.setData({
        shenfeng:event.detail.id,
        subject:event.detail.name
     })
  },
  getDate:function(event){
    this.setData({
      date:event.detail.d
    })
  },
  getSubject:function(event){
    console.log(event)
     this.setData({
       subject:event.detail.value
     })
  },
  yanzhen:function(event){
    wx.navigateTo({
      url: '/fabu/duanxin/duanxin',
    })
  },
  uploadImg:function(event){
    // wx.showModal({
    //   title: '',
    //   content: '上传您的身份证正面照或相关证件照片',
    //   success:function(res){
        // if(res.confirm){
          console.log(event)
          this.setData({
            imgPath: event.detail.path.tempFilePaths[0]
          })
          console.log(this.data.imgPath)
          var that = this
          wx.uploadFile({
            url: app.globalData.baseUrl + 'file/wechat',
            filePath: that.data.imgPath,
            name: 'files',
            header: {
              'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
              'content-type': 'application/x-www-form-urlencoded'
            },
            formData: {
              // files:'老师证件照',
              'modular': 11
            },
            method: 'POST',
            success: function (res) {
              console.log(res)
              var data = JSON.parse(res.data)
              console.log(data)
              that.setData({
                url: data.data
              })
              console.log(that.data.url)
              wx.showToast({
                title: '上传照片成功',
              })
            },
            fail: function (res) {
              console.log(res)
              wx.showToast({
                title: '上传照片失败',
                icon: 'none'
              })
            }

          })
        // }
    //   }
    // })
    
  },
  getIntroduce:function(res){
     this.setData({
       introduce:res.detail.value
     })
  },
  getMotto:function(res){
      this.setData({
        motto:res.detail.value
      })
  },
  submit:function(res){
    var that=this
    // console.log(that.data.shenfeng)
    // console.log(that.data.motto)

    // console.log(that.data.subject)
    // console.log(that.data.universty)
    if(that.data.name==''){
      wx.showToast({
        title: '请输入您的真实姓名',
        icon:'none'
      })
    }else if(that.data.universty==''){
      wx.showToast({
        title: '请输入您的大学名称',
        icon: 'none'
      })
    } else if (that.data.shenfeng == '') {
      wx.showToast({
        title: '请选择您的身份',
        icon: 'none'
      })
    } else if (that.data.subject == '') {
      wx.showToast({
        title: '请输入您的擅长科目',
        icon: 'none'
      })
    } else if (that.data.introduce == '') {
      wx.showToast({
        title: '请输入您的个人简介',
        icon: 'none'
      })
    } else if (that.data.motto == '') {
      wx.showToast({
        title: '请输入您的座右铭',
        icon: 'none'
      })
    }
    else if (that.data.url == '') {
      wx.showToast({
        title: '请上传您的证件照',
        icon: 'none'
      })
    }else{
      console.log(that.data.shenfeng)
      console.log(that.data.motto)
      console.log(that.data.universty)
      console.log(that.data.subject)
      console.log(that.data.url)
      wx.request({
        url: app.globalData.baseUrl + 'user/teacher/info',
        method: 'post',
        data: {
          schoolId: 1,
          typeId: that.data.shenfeng,
          motto: that.data.motto,
          speciality: that.data.subject,
          schoolName: that.data.universty,
          experience: that.data.introduce,
          description: that.data.url,
          realName: that.data.name
        },
        header: {
          'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          if (res.data.msg == '请求成功') {
            wx.showModal({
              title: '',
              content: '申请成功，请耐心等待',
              success: function (res) {
                wx.navigateBack({

                })
              }
            })
          } else {
            wx.showModal({
              title: '申请失败',
              content: res.data.msg,
              success: function (res) {
               
              }
            })
          }

        }
      })
    }
  
  },
  chooseAdd: function (event) {
    var that = this
    var location
    var add
    //这是一个异步函数
    wx.chooseLocation({
      success: function (res) {
        var add = res.address
        console.log(add.length)
        if (add.length > 20) {
          add = res.name
        }
        that.setData({
          address: add
        })

      },
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var apply=false
    apply=options.apply
    if(!apply){
      try {
        wx.request({
          url: app.globalData.baseUrl + 'personal/apply/teacher',
          method: 'get',
          header: {
            'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
          },
          success: function (res) {


            console.log(res)
            try {
              var status = res.data.data.status
              if (status == 0 || status == 1) {
                wx.redirectTo({
                  url: '/geren/applyTeacher/applyTeacher?status=' + status,
                })
              } else if (status == 2) {
                wx.showModal({
                  title: '',
                  content: '您的申请成为老师审核失败，您可以选择重新申请',
                })
              } else {

              }
            } catch (err) {
              console.log(err)
            }

          }
        })
      } catch (err) {
        console.log(err)
      }
    }
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this 

    wx.request({
      url: app.globalData.baseUrl+'teacher/type',
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')  
      },
      success:function(res){
        console.log(res)
        for(var index=0;index<res.data.data.data.length;index++){
          var id = 'shenfenArray[' + index + '].id'
          var value = 'shenfenArray['+index+'].value'
          that.setData({
            [id]:res.data.data.data[index].id,
            [value]:res.data.data.data[index].name
          })          
        }
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})